package com.leetcode.p0;

//      implementation 'com.alibaba:arouter-api:1.5.2'
//    kapt 'com.alibaba:arouter-compiler:1.5.2'
//   compileOnly

import java.util.Stack;

public class Test71 {

    public String simplifyPath(String path) {
        String[] a = path.split("/");

        Stack<String> stack = new Stack<>();

        for (String item : a) {
            if ("".equals(item)) {
            } else if (".".equals(item)) {

            } else if ("..".equals(item)) {
                if (!stack.empty()) {
                    stack.pop();
                }
            } else {
                stack.push(item);
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        while (!stack.empty()) {
            stringBuilder.insert(0, "/" + stack.pop());
        }

        if (stringBuilder.length() == 0) {
            return "/";
        }

        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        Test71 test = new Test71();
        String result = test.simplifyPath("/a/./b/../../../c");
        System.out.println(result);
    }

}
